#include "utils_random.h"
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>

int utils_getRandomLimit(int min, int max){

    if(min >= max || min < 0 || max < 0){
        return -1;
    }

    struct timeval tv;
    gettimeofday(&tv, NULL);

    srand((unsigned int)(tv.tv_sec * 1000000 + tv.tv_usec));

    return (abs(rand()) % (1 + max - min) + min);
}

uint8_t utils_getRamdomU8(void){

    struct timeval tv;
    gettimeofday(&tv, NULL);

    srand((unsigned int)(tv.tv_sec * 1000000 + tv.tv_usec));

    return (uint8_t)(abs(rand()) % 255);
}

char *utils_getRandomHexString(int byteslength, int case_sen){

    if(byteslength <= 0)
        return NULL;

    char *retString = (char *)malloc(byteslength * 2 + 1);
    memset(retString, '\0', byteslength * 2 + 1);

    for(int i = 0; i < byteslength; i++){
        char bufChar[3];
        if(case_sen)
            sprintf(bufChar, "%02X", utils_getRamdomU8());
        else
            sprintf(bufChar, "%02x", utils_getRamdomU8());

        memcpy(retString + i * 2, bufChar, 2);
    }

    return retString;
}




